package org.example.view;

import org.example.stuview.StudentView;
import org.example.teaView.Firstview;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class RegisterUserView extends JFrame implements Runnable{

        JTextField inputID; //输入ID
        JTextField inputPassword; //输入密码
        JTextField hintMess;
        JButton submit,login;
        HandleRegisterUser handleRegisterUser;
        JPanel jPanel = new JPanel();
        public RegisterUserView(){
                initView();
                registerListener();
        }
        public void initView() {
                this.setSize(480,320);
                this.setDefaultCloseOperation(3);
                this.setLocationRelativeTo(null);
                this.setTitle("登陆界面 -3220608045吉治霖软件2202");
                //Panel;
                jPanel.setBounds(0,0,480,320);
                jPanel.setBackground(Color.white);
                jPanel.setLayout(null);
                this.getContentPane().add(jPanel);
                //ID:  设置
                Font usernameFont = new Font(null,1,16);
                JLabel usernameText = new JLabel("ID：");
                usernameText.setFont(usernameFont);
                usernameText.setBounds(100,55,55,22);
                jPanel.add(usernameText);
                //ID输入框   设置
                inputID = new JTextField();
                inputID.setBounds(180,55,150,22);
                jPanel.add(inputID);
                //密码：  设置
                Font passworadFont = new Font(null,1,16);
                JLabel passwordText = new JLabel("密码：");
                passwordText.setFont(passworadFont);
                passwordText.setBounds(100,85,55,22);
                jPanel.add(passwordText);
                //密码输入框   设置
                inputPassword = new JTextField();
                inputPassword.setBounds(180,85,150,22);
                jPanel.add(inputPassword);
                //登录按钮  设置
                submit = new JButton("登录");
                submit.setBounds(230,135,80,30);
                jPanel.add(submit);
                //注册按钮
                login = new JButton("注册");
                login.setBounds(130,135,80,30);
                jPanel.add(login);
                //提示框
                hintMess = new JTextField(20);
                hintMess.setEditable(false);
                hintMess.setBounds(90,185,250,25);
                jPanel.add(hintMess);

                this.setVisible(true);

                }
        private void registerListener() {
                handleRegisterUser = new HandleRegisterUser();
                handleRegisterUser.setRegisterUserView(this);
                submit.addActionListener(handleRegisterUser);
                login.addActionListener(handleRegisterUser);
        }

        @Override
        public void run() {                      //实时接受数据
                String s=null;
                while(true) {
                        try{
                                s=handleRegisterUser.in.readUTF();
                                if (s.equals(null)){
                                        break;
                                }
                                System.out.println(s);
                                if (s.startsWith("查询信息")){
                                        hintMess.setText(s.substring(6));
                                        if (s.substring(6).equals("老师登录成功")){
                                                handleRegisterUser.registerUserView.setVisible(false);
                                                new Firstview();
                                        }if (s.substring(6).startsWith("学生登录成功")){
                                                int ID_i = s.indexOf("ID"),name_i = s.indexOf("name");
                                                String s1 = s.substring(ID_i+3,name_i).trim(),s2 = s.substring(name_i + 5).trim();
                                                StudentView studentView = new StudentView();
                                                studentView.setStuID(s1);
                                                studentView.setStuName(s2);
                                                studentView.init();
                                                studentView.studentListener();
                                                handleRegisterUser.registerUserView.setVisible(false);
                                        }
                                }
                        }
                        catch(IOException e) {
                                break;
                        }
                }

        }
}
